/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jturbofile;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jturbofile.gui.LogPanel;

/**
 *
 * @author Rolf
 */
public class Config {
    private static final String CONFIG_FILE = "jturbofile.cfg";
    
    private static Config singleton;

    private void defaultValues() {
        setValueNoSave("path", "");
        setValueNoSave("version", "0.43");
        setValueNoSave("speed", "128");
    }
    
    private Map<String,String> values;

    private synchronized static Config loadConfig() {
        Config c = new Config();
        
        try {
            ObjectInputStream in = new ObjectInputStream( new FileInputStream( CONFIG_FILE ) );
            Map<String,String> newValues = (Map<String,String>) in.readObject();
            
            c.setValues(newValues);
        }
        catch(Exception e) {
            LogPanel.singleton().logln("Error loading config: "+e);
        }
        return c;
    }
    
    private synchronized void saveConfig() {
        try {
            ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream( CONFIG_FILE ) );
            
            out.writeObject(values);
            out.flush();
        }
        catch(Exception e) {
            LogPanel.singleton().logln("Error saving config: "+e);
        }
    }
    
    private void setValues(Map<String,String> newValues) {
        values = newValues;
    }
    
    private Config() {
        values = new HashMap<String,String>();
        defaultValues();
    }
    
    public enum Compression { NONE, GZIP, BZIP, LZMA };
    
    public Compression getCompressionType() {
        String compression = getValue("compression");
        if( compression == null ) {
            return Compression.BZIP;
        }
        
        return Compression.valueOf(compression);
    }
    
    public void setCompressionType(Compression c) {
        setValue("compression", ""+c);
    }
    
    public synchronized static Config singleton() {
        if( singleton == null ) {
            singleton = loadConfig();
        }
        
        return singleton;
    }
    
    public String getValue(String key) {
        return values.get(key);
    }
    
    public void setValueNoSave(String key, String value) {
        setValue(key, value, false);
    }

    public void setValue(String key, String value) {
        setValue(key, value, true);
    }
    
    public void setValue(String key, String value, boolean save) {
        values.put(key, value);
        
        if( save ) {
            saveConfig();
        }
    }
    
    public int getXPos() {
        return stringToInt(getValue("xpos"), 0);
    }
    
    public int getYPos() {
        return stringToInt(getValue("ypos"), 0);
    }

    public void setXPos(int xpos) {
        setValue("xpos", ""+xpos);
    }
    
    public void setYPos(int ypos) {
        setValue("ypos", ""+ypos);
    }

    private int stringToInt(String s, int def) {
        try {
            return Integer.parseInt(s);
        }
        catch(Exception e) {
            return def;
        }
    }
    
    public String getVersion() {
        return getValue("version");
    }
    
    public void setSpeed(int speed) {
        setValue("speed", ""+speed);
    }
    
    public int getSpeed() {
        return stringToInt(getValue("speed"), 16);
    }
    
    public void setHosts(List<String> hosts) {
        String hostsString = "";
        
        boolean first = true;
        
        for(String s : hosts) {
            if( first ) {
                hostsString += s;
                first = false;
            }
            else {
                hostsString += "*"+s;
            }
        }
        
        setValue("hosts", hostsString);
    }
    
    public List<String> getHosts() {
        List<String> hosts = new ArrayList<String>();
        String hostsString = getValue("hosts");
        
        if( hostsString != null ) {
            String[] split = hostsString.split("\\*");
        
            for(String s : split) {
                hosts.add(s);
            }
        }
        
        return hosts;
    }
}
